package com.zhentao.tcp1.service;

import com.zhentao.tcp1.model.ParkingEntry;
import com.zhentao.tcp1.model.ParkingOwner;
import com.zhentao.tcp1.repository.ParkingEntryRepository;
import com.zhentao.tcp1.repository.ParkingOwnerRepository;
import io.swagger.v3.oas.annotations.servers.Server;
import jakarta.transaction.Transactional;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class ParkingManagementService {
    private final ParkingEntryRepository parkingEntryRepository;
    private final ParkingOwnerRepository parkingOwnerRepository;

    public ParkingManagementService(ParkingEntryRepository parkingEntryRepository, ParkingOwnerRepository parkingOwnerRepository) {
        this.parkingEntryRepository = parkingEntryRepository;
        this.parkingOwnerRepository = parkingOwnerRepository;

    }
    @Tool(name = "recordEntry",description = "记录车辆进场信息。需要提供车牌号码和入场时间")
    @Transactional
    public ParkingEntry recordEntry(String licensePlate,String vehicleType,String parkingArea,String entryChannel,String operator,long ownerId,double preAuthAmount,String vehicleColor,String deviceSn,String remark) {
        ParkingEntry parkingEntry =ParkingEntry.builder()
                .licensePlate(licensePlate)
                .entryTime(LocalDateTime.now())
                .vehicleType(vehicleType)
                .parkingArea(parkingArea)
                .entryChannel(entryChannel)
                .operator(operator)
                .ownerId(ownerId)
                .preAuthAmount(preAuthAmount)
                .vehicleColor(vehicleColor)
                .deviceSn(deviceSn)
                .remark(remark).build();
        return parkingEntryRepository.save(parkingEntry);
    }
    @Tool(name = "recordExitAndCalculateFee",description = "记录车辆出场信息并计算停车费用。需提供车牌号码，若有多条进场记录，更新最新的未出场记录")
    @Transactional
    public String recordExitAndCalculateFee(String licensePlate){
        List<ParkingEntry> entries = parkingEntryRepository.findByLicensePlate(licensePlate);
        if (entries == null){
            return "未找到 该车辆的 相关入场信息";
        }
        Optional<ParkingEntry> lastparkingEntry=entries.stream()
                .filter(entry->entry.getRemark()==null || entry.getRemark().contains("已出场"))
                .max((e1,e2)->e1.getEntryTime().compareTo(e2.getEntryTime()));
        if (lastparkingEntry.isEmpty()){
            return "未找到该车辆 出场信息";
        }
        ParkingEntry parkingEntry=lastparkingEntry.get();
        LocalDateTime entryTime = parkingEntry.getEntryTime();
        LocalDateTime now = LocalDateTime.now();
        Duration duration = Duration.between(entryTime, now);
        long minutes = duration.toMinutes();
        double fee = minutes * 0.5;

        parkingEntry.setRemark(parkingEntry.getRemark()==null?"该车辆已出场,费用为:"+fee: parkingEntry.getRemark()+"该车辆已出场,费用为:"+fee);
        parkingEntryRepository.save(parkingEntry);
        return String.format("该车辆 %s 停车时长为 %.2f,停车时长为 %d",licensePlate,fee,minutes);
    }
    @Tool(name = "getParkingEntries",description = "根据车牌号查询所有进场记录，返回记录列表")
    public List<ParkingEntry> getParkingEntries(String licensePlate){
        return parkingEntryRepository.findByLicensePlate(licensePlate);
    }
    @Tool(name = "getParkingOwner",description = "根据车牌号查询车主信息")
    public Optional<ParkingOwner> getParkingOwner(String licensePlate){
        return parkingOwnerRepository.findByLicensePlate(licensePlate);
    }
    @Tool(name = "registerOwner",description = "注册或更新车主信息，需提供车牌、姓名、电话和会员等级")
    public ParkingOwner registerOwner(String licensePlate,String ownerName,String phone,String memberLevel){
        Optional<ParkingOwner> plate = parkingOwnerRepository.findByLicensePlate(licensePlate);
        ParkingOwner owner;
        if (plate.isPresent()){
            owner=plate.get();
            owner.setOwnerName(ownerName);
            owner.setPhone(phone);
            owner.setMemberLevel(memberLevel);
        }else {
            owner=ParkingOwner.builder()
                    .licensePlate(licensePlate)
                    .ownerName(ownerName)
                    .phone(phone)
                    .memberLevel(memberLevel)
                    .build();
        }
        return parkingOwnerRepository.save(owner);
    }
}
